package com.bike.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.fastjson.JSONObject;
import com.bike.entity.HhBusiness;
import com.bike.entity.HhBusinessUser;
import com.bike.service.impl.HhBusinessServiceImpl;
import com.bike.service.impl.HhBusinessUserServiceImpl;
import com.bike.util.Constant;

public class LoginInterceptor extends HandlerInterceptorAdapter{
	@Autowired
	private HhBusinessUserServiceImpl businessUserServiceImpl;
	@Autowired
	private HhBusinessServiceImpl businessServiceImpl;
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		
		try {
			String path=request.getRequestURI();
			String servletPath=request.getServletPath();
			
			String []  endpassArr=new String[]{
					"/login/toLogin","/h5login","/login/doLogin","/apiLogin/sendCode","/apiLogin/login","/apiLogin/reg","/apiLogin/version"
					,"/apiLogin/rePwd"
			};
			
			//登录界面全部通过
			for(String pass:endpassArr){
				if(servletPath.startsWith(pass)){
					return true;
				}
			}
			
			
			//支付宝 微信回调 
			if(servletPath.startsWith("/authc")){
				return true;
			}
			//pc app  接口
			if(servletPath.startsWith("/api")){
				if(Constant.debug){
					return true;
				}
			 
				//接口
				String userId=request.getParameter("userId");
				String token=request.getParameter("token").trim();
				if(token.equals("")){
					return false;
				}
				HhBusinessUser businessUser=businessUserServiceImpl.selectByHhBusinessUserId(Long.parseLong(userId));
				
				
				int code=1;
				String msg="参数错误";
				//if(businessUser!=null&&businessUser.getToken().equals(token)){
				if(businessUser!=null){
					if(businessUser.getStatus()==0){//未被删除
						String businessId=request.getParameter("businessId");
						if(businessId!=null){
							if(businessUser.getType()==Constant.unionType){
								long businessIdLong=Long.parseLong(businessId);
								HhBusiness business=businessServiceImpl.selectByHhBusinessId(businessIdLong);
								if(business.getParentId().longValue()==businessUser.getBusinessId()){
									request.setAttribute("business", business);
									businessUser.setBusinessId(businessIdLong);
								}
							}
						}
						request.setAttribute("businessUser", businessUser);
						request.setAttribute("userId", userId);
						request.setAttribute("token", token);
						return true;
					}else{
						code=446;
						msg="登录信息异常,请重新登录!";
					}
				}
				response.setContentType("text/html;charset=utf-8" );
				
				Map<String, Object> result=new HashMap<>();
				result.put("code", code);
				result.put("msg", msg);
				response.getWriter().write(JSONObject.toJSONString(result));//直接将完整的表单html输出到页面
				response.getWriter().flush();
				response.getWriter().close();
				return false;
			}
			//h5 扫码
			if(servletPath.startsWith("/j/")){
				return true;
			}
			//h5 扫码相关接口  j,jhh开头
			if(servletPath.startsWith("/jhh")){
				if(request.getSession().getAttribute(Constant.h5User)!=null){
					return true;
				}
				response.sendRedirect(request.getContextPath() +  "/j/"+request.getParameter("diningId"));
				return false;
			}else{
				if(request.getSession().getAttribute("user")!=null){
					return true;
				}
				response.sendRedirect(request.getContextPath() +  "/login/toLogin");
				return false;
			}
		} catch (Exception e) {
		}
		return false;
	}

}
